package com.aaa.product.oauth2;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Value("${security.oauth2.client.client-id}")
    private String clientId;

    @Value("${security.oauth2.client.client-secret}")
    private String secret;

    @Value("${security.oauth2.authorization.check-token-access}")
    private String checkTokenEndpointUrl;

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    /**
     * @Description OAuth2 token持久化接口
     * @Date 2019/7/15 18:12
     * @Version  1.0
     */
    @Bean
    public TokenStore tokenStore() {
        return new RedisTokenStore(redisConnectionFactory);
    }

    /**
     * @Description 令牌服务
     * @Date 2019/7/15 18:07
     * @Version  1.0
     */
    @Bean
    @Primary
    public RemoteTokenServices tokenService() {
        RemoteTokenServices tokenService = new RemoteTokenServices();
        tokenService.setClientId(clientId);
        tokenService.setClientSecret(secret);
        tokenService.setCheckTokenEndpointUrl(checkTokenEndpointUrl);
        return tokenService;
    }
    /*
     * 把token验证失败后，重新刷新token的类设置到 OAuth2AuthenticationProcessingFilter
     * token验证过滤器中
     * */
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenServices(tokenService());
        //资源ID
        // resources.resourceId("resource_password_id");
        super.configure(resources);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        // 配置访问控制，必须认证后才可以访问
        http.authorizeRequests()
                .antMatchers("/product/findProductPicByPath").permitAll()
                .antMatchers("/product/updateProductPic").permitAll()
                .antMatchers("/product/insertProductPic").permitAll()
                .antMatchers("/activity/findProductPicByPath").permitAll()
                .antMatchers("/activity/updateProductPic").permitAll()
                .antMatchers("/activity/insertProductPic").permitAll()
                .antMatchers("/activity/findTbActProductByTid").permitAll()
                .antMatchers("/product/selectAll").permitAll()
                .antMatchers("/product/findById/**").permitAll()
                .antMatchers("/seckill/getNowTimeActivity").permitAll()
                .antMatchers("/seckill/selectTbActInfByNowTimeAndTid").permitAll()
                .antMatchers("/seckill/selectTbActInfByNowTimeAndPid").permitAll()
                .anyRequest().authenticated();
    }


    /**
     * @Description 加密方式
     * @Date 2019/7/15 18:06
     * @Version  1.0
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
